package com.tangcco.lockwell.controller.admin;

import com.tangcco.lockwell.base.RestResponse;
import com.tangcco.lockwell.domain.TopicNalysis;
import com.tangcco.lockwell.service.TopicNalysisService;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;

import static com.tangcco.lockwell.utility.DateToString.dateToString;

@RestController("TopicNalysisController")
@RequestMapping(value = "/api/admin/topicNalysis")
public class TopicNalysisController {
    private final TopicNalysisService topicNalysisService;

    public TopicNalysisController(TopicNalysisService topicNalysisService) {
        this.topicNalysisService = topicNalysisService;
    }
    @RequestMapping("/add")
    public RestResponse insert(@RequestBody @Valid TopicNalysis model) {
        if (model.getId() == null) {
            model.setCreateTime(dateToString(new Date()));
            topicNalysisService.insert(model);
        } else {
            model.setModifyTime(dateToString(new Date()));
            topicNalysisService.updateByIdFilter(model);
        }
        return RestResponse.ok(model.getId());
    }
    @RequestMapping("/delete/{id}")
    public RestResponse deleteById(@PathVariable Integer id) {
        topicNalysisService.deleteById(id);
        return RestResponse.ok();
    }
    @RequestMapping("/select/{id}")
    public RestResponse selectById(@PathVariable Integer id) {
        TopicNalysis topicNalysis = topicNalysisService.selectById(id);
        return RestResponse.ok(topicNalysis);
    }
}
